<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title-container">
        <h3 class="title">vite + vue + ts</h3>
      </div>
      <el-form
          ref="loginFormRef"
          :model="formData"
          status-icon
          :rules="rules"
          class="loginFormRef"
      >
        <el-form-item prop="account">
          <el-input
              v-model="formData.account"
              type="text"
              autocomplete="off"
              placeholder="请输入用户名"
          >
            <template #prepend>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>

        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="formData.password"
              type="password"
              autocomplete="off"
              placeholder="请输入密码"
          >
            <template #prepend>
              <el-icon>
                <Key/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="login" class="login_btn"
          >登陆
          </el-button>
        </el-form-item>
      </el-form>
      <!--滑动验证码-->
      <CustomCaptcha
          :show="state.isShowCaptcha"
          @success="verifySuccess"
          @fail="verifyFail"
          @close="closeCaptcha"
      />
    </div>
  </div>
</template>

<script lang="ts" setup name="Login">
import {reactive, ref, toRefs} from 'vue'
import {FormInstance} from "element-plus";
import {UserService} from "../../api/user/login";
import {Session} from "@/utils/storage";
import {common} from "@/constance/common";
import {useRouter} from "vue-router";


const loading = ref(false)
const state = reactive({
  //是否显示验证码
  isShowCaptcha: false,
  formData: {
    account: 'admin',
    password: '123456'
  },
  rules: {
    account: [
      {
        required: true,
        message: '请输入用户名！',
        trigger: 'blur',
      },
    ],
    password: [
      {
        required: true,
        message: '请输入密码！',
        trigger: 'blur',
      },
      {min: 6, max: 8, message: '密码长度应在 6 ～ 8 之间', trigger: 'blur'}
    ]
  }
})
const loginFormRef = ref<FormInstance>()
let {formData, rules} = toRefs(state)

//关闭滑动弹窗
const closeCaptcha = () => {
  state.isShowCaptcha = false
}

//验证成功
const verifySuccess = () => {
  UserService.login(state.formData)
      .then(({data}) => {
        //模拟路由数据
        let menuArr = [
          {
            id: 1,
            parentId: 0,
            title: '首页',
            name: 'Home',
            icon: 'HomeFilled',
            path: '/home',
            component: 'home/index',
            type: 1,
          },
          {
            id: 2,
            parentId: 0,
            title: '起步',
            name: 'Start',
            icon: 'Sunrise',
            path: '/start',
            component: '',
            type: 0,
            children: [
              {
                id: 3,
                parentId: 2,
                title: '第一个页面',
                name: 'TheFirst',
                icon: 'Link',
                path: '/start/theFirst',
                component: 'start/theFirst/index',
                type: 1,
              },

            ]
          },
          {
            id: 4,
            parentId: 0,
            title: 'ElementPlus组件',
            name: 'TestPlus',
            icon: 'Star',
            path: '/testPlus',
            component: '',
            type: 0,
            children: [
              {
                id: 5,
                parentId: 4,
                title: '树形表格',
                name: 'TreeDemo',
                icon: 'Pointer',
                path: '/testPlus/treeDemo',
                component: 'testPlus/treeDemo/index',
                type: 1,
              },
              {
                id: 6,
                parentId: 4,
                title: 'Icon选择',
                name: 'IconSelector',
                icon: 'Monitor',
                path: '/testPlus/iconSelector',
                component: 'testPlus/iconSelector/index',
                type: 1,
              },
              {
                id: 7,
                parentId: 4,
                title: '4K图片',
                name: 'FileUpDown',
                icon: 'Folder',
                path: '/testPlus/fileUpDown',
                component: 'testPlus/fileUpDown/index',
                type: 1,
              },
              {
                id: 8,
                parentId: 4,
                title: '在线影院',
                name: 'HotMovie',
                icon: 'VideoCamera',
                path: '/testPlus/hotMovie',
                component: 'testPlus/hotMovie/index',
                type: 1,
              },
            ]
          },

          {
            id: 9,
            parentId: 0,
            title: '错误页',
            name: 'ErrorPage',
            icon: 'FolderDelete',
            path: '/error',
            component: '',
            type: 0,
            children: [
              {
                id: 10,
                parentId: 9,
                title: '404',
                name: '404',
                icon: 'DocumentDelete',
                path: '/error/404',
                component: '404/index',
                type: 1,
              },
              {
                id: 11,
                parentId: 9,
                title: '403',
                name: '403',
                icon: 'Lock',
                path: '/error/403',
                component: '403/index',
                type: 1,
              },
            ]
          }
        ]
        Session.set(common.menuTree, JSON.stringify(menuArr))
        Session.set(common.userInfo, JSON.stringify(data.resultObj))
        loading.value = false
        router.push({path: '/'})
      })
      .catch(() => {
        state.isShowCaptcha = false
        loading.value = false
      })
}

//验证失败
const verifyFail = () => {
  loading.value = false
}

let router = useRouter();

//登陆操作
const login = () => {
  loginFormRef.value?.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      state.isShowCaptcha = true
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style scoped lang='less'>
.login-container {
  width: 100%;
  height: 100%;
  background: url("../../assets/image/th.jpeg") no-repeat center center;
  background-size: 100% 100%;
  position: relative;

  .login-box {
    z-index: 100;
    width: 27%;
    background: rgba(0, 0, 0, 0.3);
    position: absolute;
    right: 10%;
    bottom: 20%;
    opacity: 0.8; //透明度
    padding: 30px 0;

    .title-container {
      position: relative;
      margin-bottom: 20px;

      .title {
        color: white;
        font-size: 28px;
        text-align: center;
      }
    }

    .loginFormRef {
      position: relative;
      width: 75%;
      margin: 0 auto; //水平居中
      //vertical-align: top
      //overflow: hidden;

      .login_btn {
        width: 100%;
      }
    }
  }
}
</style>
